package com.example.myapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.app.Activity;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.TextView;
import android.widget.Toast;

import com.example.myapplication.R;

public class MainActivity extends Activity {

    //需要适配的数据
    private String[] titles={"原神","双相","明日方舟","箱庭小偶（测试服）","碧蓝航线","战双帕弥什"};
    private String[] prices={"米哈游","西山居游戏","鹰角网络","bilibili游戏","bilibili游戏","库洛游戏"};
    //图片集合
    private  int[] icons={R.drawable.img_1, R.drawable.img_2, R.drawable.img_3,
            R.drawable.img_4, R.drawable.img_5, R.drawable.img_6};
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //初始化ListView控件
        ListView listView=findViewById(R.id.lv);
        //创建一个Adapter的实例
        MyBaseAdapter mAdapter=new MyBaseAdapter();
        //设置Adapter
        listView.setAdapter(mAdapter);

        Button buttonMore = findViewById(R.id.bt_more);
        buttonMore.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(MainActivity.this,MessageActivity.class);
                startActivity(intent);
            }
        });

    }
    class MyBaseAdapter extends BaseAdapter{

        @Override
        public int getCount(){       //得到item的总数
            return titles.length;
        }

        @Override
        public Object getItem(int position){
            return titles[position]; //返回item的数据对象
        }
        @Override
        public long getItemId(int position){
            return position;         //返回item的id
        }
        @Override
        public View getView(int position, View convertView, ViewGroup parent){//获取item中的View视图
            ViewHolder holder;
            if(convertView==null){
                convertView=View.inflate(MainActivity.this,R.layout.list_item, null);
                holder=new ViewHolder();
                holder.title=convertView.findViewById(R.id.title);
                holder.price=convertView.findViewById(R.id.price);
                holder.iv=convertView.findViewById(R.id.iv);
                holder.iv.setOnClickListener(new View.OnClickListener(){
                    @Override
                    public void onClick(View view) {
                        //Toast.makeText(getApplicationContext(), "你点击了按钮", Toast.LENGTH_SHORT).show();
                        Intent intent = new Intent(MainActivity.this,DetailActivity.class);
                        intent.putExtra("position",position);
                        startActivity(intent);
                    }
                });
                convertView.setTag(holder);
            }else{
                holder=(ViewHolder)convertView.getTag();
            }
            holder.title.setText(titles[position]);
            holder.price.setText(prices[position]);
            holder.iv.setImageResource(icons[position]);
            return convertView;
        }
    }
    class ViewHolder{
        TextView title;
        TextView price;
        ImageView iv;
    }
}

